using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Habanero.Base;
using Habanero.BO;

namespace FireStarterModeller.BO
{
    public class FirestarterUtils
    {
        //TODO  05 Mar 2009: Make this internal and set up as friend
        public static int GetNextUIOrder(BusinessObjectCollection<DMProperty> properties)
        {
            BusinessObjectCollection<DMProperty> orderableItems = properties.Clone(); // So that it does not modify the original collection
            int maxUIOrder = 0;
            orderableItems.Sort("OrdinalPosition", true, true);
            if (orderableItems.Count > 0)
            {
                DMProperty lastProp = orderableItems[orderableItems.Count - 1];
                maxUIOrder = lastProp.Order + 1;
                if (maxUIOrder < orderableItems.Count)
                {
                    maxUIOrder = orderableItems.Count;
                }
            }

            foreach (DMProperty dmProperty in properties.RemovedBusinessObjects)
            {
                if (maxUIOrder <= dmProperty.Order)
                {
                    maxUIOrder = dmProperty.Order +1;
                }
            }
            foreach (DMProperty dmProperty in properties.MarkedForDeleteBusinessObjects)
            {
                if (maxUIOrder <= dmProperty.Order)
                {
                    maxUIOrder = dmProperty.Order +1;
                }
            }
            return maxUIOrder;
        }

        public static int GetMaxFieldNumber(BusinessObjectCollection<DMProperty> properties)
        {
            int maxFieldNumber = 0;
            foreach (DMProperty property in properties)
            {
                if (!String.IsNullOrEmpty(property.MappedDBColumnName) && property.MappedDBColumnName.StartsWith("field"))
                {
                    string fieldNumStr = property.MappedDBColumnName.Substring("field".Length);
                    int fieldNum = Convert.ToInt32(fieldNumStr);
                    if (fieldNum > maxFieldNumber) maxFieldNumber = fieldNum;
                }
            }
            return maxFieldNumber;
        }

        internal static void SwapOrder(DMProperty item1, DMProperty item2)
        {
            int tempUIOrder = item1.Order;
            item1.OrdinalPosition = item2.Order;
            item2.OrdinalPosition = tempUIOrder;
        }

        internal static void SwapObjectWithPrevious(IList<DMProperty> sortedItems, DMProperty item)
        {
            DMProperty previousItem = null;
            foreach (DMProperty currentItem in sortedItems)
            {
                if (currentItem == item) break;
                previousItem = currentItem;
            }
            if (previousItem == null) return;

            SwapOrder(item, previousItem);
        }

        public static int GetNextSortPositionOrder(BusinessObjectCollection<DMSortProperty> orderableItems)
        {
            int maxUIOrder = 0;
            orderableItems.Sort("SortPosition", true, true);
            if (orderableItems.Count > 0)
            {
                DMSortProperty lastProp = orderableItems[orderableItems.Count - 1];
                maxUIOrder = lastProp.Order + 1;
                if (maxUIOrder < orderableItems.Count)
                {
                    maxUIOrder = orderableItems.Count;
                }
            }

            foreach (DMSortProperty dmSortProperty in orderableItems.RemovedBusinessObjects)
            {
                if (maxUIOrder <= dmSortProperty.Order)
                {
                    maxUIOrder = dmSortProperty.Order + 1;
                }
            }
            foreach (DMSortProperty dmSortProperty in orderableItems.MarkedForDeleteBusinessObjects)
            {
                if (maxUIOrder <= dmSortProperty.Order)
                {
                    maxUIOrder = dmSortProperty.Order + 1;
                }
            }
            return maxUIOrder;
        }

    }
    public interface IOrderableBusinessObject
    {
        Int32 Order { get; set; }

        IBusinessObject BusinessObject { get; }
    }
}
